package com.ygxsk.carhome.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.ygxsk.carhome.R;
import com.ygxsk.carhome.alertview.AlertView;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.permission.PermissionSetting;
import com.ygxsk.carhome.response.TimeLineModel;
import com.ygxsk.carhome.utils.CommUtils;

import java.util.ArrayList;

/**
 * Created by itchenqi on 2018/3/31 0031.
 * description：报修进度列表内容的时间轴适配器
 */
public class OrderProgrAdapter extends BaseAdapter {

    private Activity activity;//
    private Context context;//

    private LayoutInflater mLayoutInflater;
    private ArrayList<TimeLineModel> filesReplace;

    //private Rationale mRationale;
    private PermissionSetting mSetting;

    private AlertView mAlertView;

    public OrderProgrAdapter(Activity _activity, Context _context, ArrayList<TimeLineModel> _filesReplace) {
        this.activity = _activity;
        this.context = _context;
        this.filesReplace = _filesReplace;

        mLayoutInflater = CommUtils.getLayoutInflater(_context);

        //权限相关两个。
        //mRationale = new DefaultRationale();
        mSetting = new PermissionSetting(_context);
    }

    @Override
    public int getCount() {
        return filesReplace == null ? 0 : filesReplace.size();
    }

    @Override
    public Object getItem(int position) {
        if (filesReplace != null && position < getCount()) {
            return filesReplace.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /*public void clearData() {

        if (filesReplace != null) {
            if (!filesReplace.isEmpty()) {
                filesReplace.clear();
            }
            notifyDataSetChanged();
        }

    }*/


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        OrderViewHolder viewHolder = null;

        if (convertView == null) {

            viewHolder = new OrderViewHolder();

            convertView = mLayoutInflater.inflate(R.layout.orderprogressadapter, parent, false);

            //viewHolder.stateTimeTop = (TextView) convertView.findViewById(R.id.textViewTimetop);
            viewHolder.layoutContentInLine = (LinearLayout) convertView.findViewById(R.id.layoutcontentinline);

            // 上中下三个线条布局
            viewHolder.imageLineTop = (ImageView) convertView.findViewById(R.id.imagetop);// 上边的轴
            viewHolder.imageLineMiddle = (ImageView) convertView.findViewById(R.id.imagemiddle);// 中间的图片
            viewHolder.imageLineBottom = (ImageView) convertView.findViewById(R.id.imagebottom);// 底部的线条轴

            viewHolder.textviewTimelineDate = (TextView) convertView.findViewById(R.id.text_timeline_date);//
            viewHolder.textviewStateLine = (TextView) convertView.findViewById(R.id.textviewstateline);//
            viewHolder.textviewTimelineContent = (TextView) convertView.findViewById(R.id.text_timeline_title);//
            viewHolder.callInRepair = (ImageView) convertView.findViewById(R.id.imagecallinrepair);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (OrderViewHolder) convertView.getTag();
        }

        //filldata
        final TimeLineModel timeLineModel = filesReplace.get(position);
        String mDate = timeLineModel.getCreateTime();
        String mTitle = timeLineModel.getTitle();
        String mContent = timeLineModel.getContent();
        final String mPhoneNumber = timeLineModel.getEngineerPhone();


        //是不是得要分开成两种,只有一条数据,大于一条数据
        int length = filesReplace.size();

        if (length == 1) {//只有一条数据

            viewHolder.imageLineTop.setVisibility(View.VISIBLE);
            viewHolder.imageLineMiddle.setVisibility(View.VISIBLE);
            viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_active));
            viewHolder.imageLineBottom.setVisibility(View.INVISIBLE);


        } else if (length > 1) {//多条数据

            if (position == 0) {//首条

                viewHolder.imageLineTop.setVisibility(View.INVISIBLE);
                viewHolder.imageLineMiddle.setVisibility(View.VISIBLE);
                viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_active));
                viewHolder.imageLineBottom.setVisibility(View.VISIBLE);

            } else if (position == (filesReplace.size() - 1)) {//末条

                viewHolder.imageLineTop.setVisibility(View.VISIBLE);
                viewHolder.imageLineMiddle.setVisibility(View.VISIBLE);
                viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_inactive));
                viewHolder.imageLineBottom.setVisibility(View.INVISIBLE);

            } else {

                viewHolder.imageLineTop.setVisibility(View.VISIBLE);
                viewHolder.imageLineMiddle.setVisibility(View.VISIBLE);
                viewHolder.imageLineMiddle.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ic_marker_inactive));
                viewHolder.imageLineBottom.setVisibility(View.VISIBLE);

            }

        }


        /*int flagTop = timeLineModel.getFlagTop();
        if (flagTop==1){//普通的
            viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_inactive));
        }else if(flagTop==2){//特殊的
            viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_active));
        }else{
            viewHolder.imageLineMiddle.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_marker_inactive));
        }*/

        CommUtils.setText(viewHolder.textviewTimelineDate, mDate);
        CommUtils.setText(viewHolder.textviewStateLine, mTitle);
        CommUtils.setText(viewHolder.textviewTimelineContent, mContent);

        if (!TextUtils.isEmpty(mPhoneNumber)) {

            viewHolder.callInRepair.setVisibility(View.VISIBLE);

            //2022年8月3日17:10:47 因为监管整改 去掉拨打电话的功能；
            //这个只有当电话号码不是空才可点击有事件
            //拨打电话内容
            viewHolder.layoutContentInLine.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CommUtils.checkDialog(mAlertView);

                    //二次确认弹窗提示
                    mAlertView = new AlertView(ConstantSign.ALERT_TITLE,//
                            mPhoneNumber, //
                            ConstantSign.ALERTVIEW_LEFT_CANCEL,//
                            new String[]{ConstantSign.ALERTVIEW_RIGHT_COPY_INFORMATION}, //拨打电话的右侧改为二次确认为复制手机号码让用户自己拨打
                            null, //
                            context, //
                            AlertView.Style.Alert,//
                            new com.ygxsk.carhome.alertview.OnItemClickListener() {
                                @Override
                                public void onItemClick(Object o, int position) {

                                    if (position != AlertView.CANCELPOSITION) {

                                        //拨打电话的右侧改为二次确认为复制手机号码让用户自己拨打
                                        //CommUtils.methodCopyClip(mPhoneNumber, true);
                                        context.startActivity(CommUtils.callThePhoneNumAction_dial(mPhoneNumber));

//                                          做是否已经授权的提示弹窗
//                                        if (!AndPermission.hasPermissions(activity, Permission.CALL_PHONE)) {
//
//                                            CommUtils.authorityRequest(activity,//
//                                                    ConstantApi.MATERIALDIALOG_AUTHORITY_CONTENT_CALLPHONE, //
//                                                    new AuthorityCallbackListener() {
//                                                        @Override
//                                                        public void doSomeThing() {
//
//                                                            //添加权限判断
//                                                            AndPermission.with(context)//
//                                                                    .runtime()//
//                                                                    .permission(Permission.CALL_PHONE)//单个权限
//                                                                    .rationale(new RuntimeRationale())//
//                                                                    .onGranted(new Action<List<String>>() {//
//                                                                        @Override
//                                                                        public void onAction(List<String> permissions) {
//
//                                                                            if (!TextUtils.isEmpty(mPhoneNumber)) {
//                                                                                context.startActivity(CommUtils.callThePhoneNum(mPhoneNumber));
//                                                                            } else {
//                                                                                CommUtils.displayToastShort(context, "未获取到电话信息");
//                                                                            }
//
//                                                                        }
//                                                                    })
//                                                                    .onDenied(new Action<List<String>>() {
//                                                                        @Override
//                                                                        public void onAction(List<String> permissions) {
//
//                                                                            CommUtils.displayToastShort(context, "取消了");
//
//                                                                            if (AndPermission.hasAlwaysDeniedPermission(context, permissions)) {
//                                                                                mSetting.showSetting(ConstantApi.PERMISSION_NORMAL, activity, permissions);
//                                                                            }
//
//                                                                        }
//                                                                    }).start();
//
//                                                        }
//                                                    });
//
//                                        } else {
//
//                                            if (!TextUtils.isEmpty(mPhoneNumber)) {
//                                                context.startActivity(CommUtils.callThePhoneNum(mPhoneNumber));
//                                            } else {
//                                                CommUtils.displayToastShort(context, "未获取到电话信息");
//                                            }
//
//                                        }

                                    }

                                }
                            }
                    );

                    mAlertView.setCancelable(true).show();

                }
            });

        } else {
            viewHolder.callInRepair.setVisibility(View.GONE);
            viewHolder.layoutContentInLine.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //LogUtils.d("itchen--点击消耗");
                }
            });
        }

        return convertView;
    }


    private static class OrderViewHolder {

        LinearLayout layoutContentInLine;
        ImageView callInRepair;
        TextView textviewTimelineContent;
        TextView textviewStateLine;
        TextView textviewTimelineDate;

        //左侧图片信息
        ImageView imageLineTop;
        ImageView imageLineMiddle;
        ImageView imageLineBottom;

    }


}
